package com.cfpl.admin_vegetable_service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cfpl.DTO.PageQueryConditionDTO;
import com.cfpl.admin_vegetable_service.service.VegetableEntryService;
import com.cfpl.project_common.context.BaseContext;
import com.cfpl.project_common.pojo.VegetableEntry;
import com.cfpl.project_common.result.BaseResult;
import com.cfpl.project_common.util.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * @Auther: LYP
 * @Date: 2023/11/2 - 10:22
 * @Description: com.cfpl.admin_vegetable_service.controller
 * @version: 1.0
 */
@RestController
@Slf4j
@Api(tags = "果蔬进场相关接口")
@RequestMapping("/vegetableEntry")
public class VegetableEntryController {

    @Autowired
    private VegetableEntryService vegetableEntryService;

    @ApiOperation("条件分页查询果蔬进场记录信息")
    @PostMapping("/pageQueryVegetableEntryRecord")
    public BaseResult<IPage<VegetableEntry>> pageQueryVegetableEntryRecord(@RequestBody PageQueryConditionDTO pageConditionDTO){
        IPage<VegetableEntry> page = new Page<>(pageConditionDTO.getCurrentPage(), pageConditionDTO.getPageSize());
        IPage<VegetableEntry> entryIPage = vegetableEntryService.pageQueryVegetableEntryRecord(page, pageConditionDTO.getQueryCondition());
        return BaseResult.ok(entryIPage);
    }

    @ApiOperation("插入果蔬进场记录")
    @PostMapping("/insertVegetableEntryRecord")
    public BaseResult insertVegetableEntryRecord(@RequestBody VegetableEntry vegetableEntry){
        //设置产品批次编号
        vegetableEntry.setPurchaseDate(new Date());
        vegetableEntry.setPurchaseBatch(UUIDUtils.getUUID());
        vegetableEntry.setAdminId(BaseContext.getCurrentUserId());
        vegetableEntryService.insertVegetableEntryRecord(vegetableEntry);
        return BaseResult.ok();
    }

    @ApiOperation("模糊查询产品编号ID")
    @GetMapping("/fuzzySearchPurchaseBatchID")
    public BaseResult<Map<String,String>> fuzzySearchPurchaseBatchID(@RequestParam(name = "fuzzyId") String fuzzyId){
        Map<String,String> batchIDs = vegetableEntryService.fuzzySearchPurchaseBatchID(fuzzyId);
        return BaseResult.ok(batchIDs);
    }


}
